<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="限制参与人数" prop="limitMans">
        <el-input
          v-model="queryParams.limitMans"
          placeholder="请输入限制参与人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系信息(0:关闭;1:开启)" prop="contactInfo">
        <el-input
          v-model="queryParams.contactInfo"
          placeholder="请输入联系信息(0:关闭;1:开启)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系信息(0:参与前填写;1:参与后填写)" prop="contactInfoInput">
        <el-input
          v-model="queryParams.contactInfoInput"
          placeholder="请输入联系信息(0:参与前填写;1:参与后填写)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户修改限制(0:可修改;1:不可修改)" prop="contactInfoModify">
        <el-input
          v-model="queryParams.contactInfoModify"
          placeholder="请输入用户修改限制(0:可修改;1:不可修改)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需填信息(姓名;联系电话;联系地址)" prop="needInfo">
        <el-input
          v-model="queryParams.needInfo"
          placeholder="请输入需填信息(姓名;联系电话;联系地址)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话短信验证(0:关闭;1:开启)" prop="contactInfoSms">
        <el-input
          v-model="queryParams.contactInfoSms"
          placeholder="请输入电话短信验证(0:关闭;1:开启)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总题量" prop="totalQuestions">
        <el-input
          v-model="queryParams.totalQuestions"
          placeholder="请输入总题量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷题量" prop="examQuestions">
        <el-input
          v-model="queryParams.examQuestions"
          placeholder="请输入试卷题量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目分数" prop="questionPoints">
        <el-input
          v-model="queryParams.questionPoints"
          placeholder="请输入题目分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷总分" prop="examPoints">
        <el-input
          v-model="queryParams.examPoints"
          placeholder="请输入试卷总分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限制秒数" prop="limitTimes">
        <el-input
          v-model="queryParams.limitTimes"
          placeholder="请输入限制秒数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参加抽奖分数" prop="awardsPoints">
        <el-input
          v-model="queryParams.awardsPoints"
          placeholder="请输入参加抽奖分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总抽奖限制次数" prop="totalAwardsTimes">
        <el-input
          v-model="queryParams.totalAwardsTimes"
          placeholder="请输入总抽奖限制次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每日每人抽奖次数" prop="everyDateAwardsTimes">
        <el-input
          v-model="queryParams.everyDateAwardsTimes"
          placeholder="请输入每日每人抽奖次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总参与机会限制次数" prop="totalJoinTimes">
        <el-input
          v-model="queryParams.totalJoinTimes"
          placeholder="请输入总参与机会限制次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每日每人参与机会次数" prop="everyDateJoinTimes">
        <el-input
          v-model="queryParams.everyDateJoinTimes"
          placeholder="请输入每日每人参与机会次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每人中奖次数限制" prop="everyoneCanAwardsTimes">
        <el-input
          v-model="queryParams.everyoneCanAwardsTimes"
          placeholder="请输入每人中奖次数限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每人每日中奖次数限制" prop="everyoneEverydateAwardsTimes">
        <el-input
          v-model="queryParams.everyoneEverydateAwardsTimes"
          placeholder="请输入每人每日中奖次数限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:buzTask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:buzTask:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:buzTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:buzTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buzTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="活动标题" align="center" prop="title" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动说明" align="center" prop="memo" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="参与人数限制" align="center" prop="joinInType" />
      <el-table-column label="限制参与人数" align="center" prop="limitMans" />
      <el-table-column label="可参与地区(0:全部;1:部分地区)" align="center" prop="joinInAreaType" />
      <el-table-column label="联系信息(0:关闭;1:开启)" align="center" prop="contactInfo" />
      <el-table-column label="联系信息(0:参与前填写;1:参与后填写)" align="center" prop="contactInfoInput" />
      <el-table-column label="用户修改限制(0:可修改;1:不可修改)" align="center" prop="contactInfoModify" />
      <el-table-column label="需填信息(姓名;联系电话;联系地址)" align="center" prop="needInfo" />
      <el-table-column label="电话短信验证(0:关闭;1:开启)" align="center" prop="contactInfoSms" />
      <el-table-column label="答题类型(0:随机出题;1:固定顺序)" align="center" prop="examType" />
      <el-table-column label="总题量" align="center" prop="totalQuestions" />
      <el-table-column label="试卷题量" align="center" prop="examQuestions" />
      <el-table-column label="题目分数" align="center" prop="questionPoints" />
      <el-table-column label="试卷总分" align="center" prop="examPoints" />
      <el-table-column label="答题时间类型(0:不限;1:总时间限制;2:每题时间)" align="center" prop="answerTimeType" />
      <el-table-column label="限制秒数" align="center" prop="limitTimes" />
      <el-table-column label="判断对错时机(0:选择时判断;1:提交时判断)" align="center" prop="judgeType" />
      <el-table-column label="参加抽奖分数" align="center" prop="awardsPoints" />
      <el-table-column label="总抽奖机会(0:不限;1:限制)" align="center" prop="totalAwardsTimesType" />
      <el-table-column label="总抽奖限制次数" align="center" prop="totalAwardsTimes" />
      <el-table-column label="每日每人抽奖次数" align="center" prop="everyDateAwardsTimes" />
      <el-table-column label="总参与机会(0:不限;1:限制)" align="center" prop="totalJoinTimesType" />
      <el-table-column label="总参与机会限制次数" align="center" prop="totalJoinTimes" />
      <el-table-column label="每日每人参与机会次数" align="center" prop="everyDateJoinTimes" />
      <el-table-column label="每人中奖次数限制" align="center" prop="everyoneCanAwardsTimes" />
      <el-table-column label="每人每日中奖次数限制(0:不限制;1:限制)" align="center" prop="everyoneEverydateAwardsType" />
      <el-table-column label="每人每日中奖次数限制" align="center" prop="everyoneEverydateAwardsTimes" />
      <el-table-column label="抽奖模式(0:按时间均匀发放)" align="center" prop="awardType" />
      <el-table-column label="主办单位" align="center" prop="organizer" />
      <el-table-column label="主办单位网站链接" align="center" prop="organizerWebsite" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:buzTask:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:buzTask:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动说明" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="限制参与人数" prop="limitMans">
          <el-input v-model="form.limitMans" placeholder="请输入限制参与人数" />
        </el-form-item>
        <el-form-item label="联系信息(0:关闭;1:开启)" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入联系信息(0:关闭;1:开启)" />
        </el-form-item>
        <el-form-item label="联系信息(0:参与前填写;1:参与后填写)" prop="contactInfoInput">
          <el-input v-model="form.contactInfoInput" placeholder="请输入联系信息(0:参与前填写;1:参与后填写)" />
        </el-form-item>
        <el-form-item label="用户修改限制(0:可修改;1:不可修改)" prop="contactInfoModify">
          <el-input v-model="form.contactInfoModify" placeholder="请输入用户修改限制(0:可修改;1:不可修改)" />
        </el-form-item>
        <el-form-item label="需填信息(姓名;联系电话;联系地址)" prop="needInfo">
          <el-input v-model="form.needInfo" placeholder="请输入需填信息(姓名;联系电话;联系地址)" />
        </el-form-item>
        <el-form-item label="电话短信验证(0:关闭;1:开启)" prop="contactInfoSms">
          <el-input v-model="form.contactInfoSms" placeholder="请输入电话短信验证(0:关闭;1:开启)" />
        </el-form-item>
        <el-form-item label="总题量" prop="totalQuestions">
          <el-input v-model="form.totalQuestions" placeholder="请输入总题量" />
        </el-form-item>
        <el-form-item label="试卷题量" prop="examQuestions">
          <el-input v-model="form.examQuestions" placeholder="请输入试卷题量" />
        </el-form-item>
        <el-form-item label="题目分数" prop="questionPoints">
          <el-input v-model="form.questionPoints" placeholder="请输入题目分数" />
        </el-form-item>
        <el-form-item label="试卷总分" prop="examPoints">
          <el-input v-model="form.examPoints" placeholder="请输入试卷总分" />
        </el-form-item>
        <el-form-item label="限制秒数" prop="limitTimes">
          <el-input v-model="form.limitTimes" placeholder="请输入限制秒数" />
        </el-form-item>
        <el-form-item label="参加抽奖分数" prop="awardsPoints">
          <el-input v-model="form.awardsPoints" placeholder="请输入参加抽奖分数" />
        </el-form-item>
        <el-form-item label="总抽奖限制次数" prop="totalAwardsTimes">
          <el-input v-model="form.totalAwardsTimes" placeholder="请输入总抽奖限制次数" />
        </el-form-item>
        <el-form-item label="每日每人抽奖次数" prop="everyDateAwardsTimes">
          <el-input v-model="form.everyDateAwardsTimes" placeholder="请输入每日每人抽奖次数" />
        </el-form-item>
        <el-form-item label="总参与机会限制次数" prop="totalJoinTimes">
          <el-input v-model="form.totalJoinTimes" placeholder="请输入总参与机会限制次数" />
        </el-form-item>
        <el-form-item label="每日每人参与机会次数" prop="everyDateJoinTimes">
          <el-input v-model="form.everyDateJoinTimes" placeholder="请输入每日每人参与机会次数" />
        </el-form-item>
        <el-form-item label="每人中奖次数限制" prop="everyoneCanAwardsTimes">
          <el-input v-model="form.everyoneCanAwardsTimes" placeholder="请输入每人中奖次数限制" />
        </el-form-item>
        <el-form-item label="每人每日中奖次数限制" prop="everyoneEverydateAwardsTimes">
          <el-input v-model="form.everyoneEverydateAwardsTimes" placeholder="请输入每人每日中奖次数限制" />
        </el-form-item>
        <el-form-item label="主办单位" prop="organizer">
          <el-input v-model="form.organizer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="主办单位网站链接" prop="organizerWebsite">
          <el-input v-model="form.organizerWebsite" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuzTask, getBuzTask, delBuzTask, addBuzTask, updateBuzTask } from "@/api/system/buzTask";

export default {
  name: "BuzTask",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      buzTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        startTime: null,
        endTime: null,
        memo: null,
        status: null,
        joinInType: null,
        limitMans: null,
        joinInAreaType: null,
        contactInfo: null,
        contactInfoInput: null,
        contactInfoModify: null,
        needInfo: null,
        contactInfoSms: null,
        examType: null,
        totalQuestions: null,
        examQuestions: null,
        questionPoints: null,
        examPoints: null,
        answerTimeType: null,
        limitTimes: null,
        judgeType: null,
        awardsPoints: null,
        totalAwardsTimesType: null,
        totalAwardsTimes: null,
        everyDateAwardsTimes: null,
        totalJoinTimesType: null,
        totalJoinTimes: null,
        everyDateJoinTimes: null,
        everyoneCanAwardsTimes: null,
        everyoneEverydateAwardsType: null,
        everyoneEverydateAwardsTimes: null,
        awardType: null,
        organizer: null,
        organizerWebsite: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "活动标题不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listBuzTask(this.queryParams).then(response => {
        this.buzTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        startTime: null,
        endTime: null,
        memo: null,
        status: null,
        joinInType: null,
        limitMans: null,
        joinInAreaType: null,
        contactInfo: null,
        contactInfoInput: null,
        contactInfoModify: null,
        needInfo: null,
        contactInfoSms: null,
        examType: null,
        totalQuestions: null,
        examQuestions: null,
        questionPoints: null,
        examPoints: null,
        answerTimeType: null,
        limitTimes: null,
        judgeType: null,
        awardsPoints: null,
        totalAwardsTimesType: null,
        totalAwardsTimes: null,
        everyDateAwardsTimes: null,
        totalJoinTimesType: null,
        totalJoinTimes: null,
        everyDateJoinTimes: null,
        everyoneCanAwardsTimes: null,
        everyoneEverydateAwardsType: null,
        everyoneEverydateAwardsTimes: null,
        awardType: null,
        organizer: null,
        organizerWebsite: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuzTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuzTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuzTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动信息编号为"' + ids + '"的数据项？').then(function() {
        return delBuzTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/buzTask/export', {
        ...this.queryParams
      }, `buzTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
